/**
 * 
 */
package com.wipz.eoa.iphone.businessProcess;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.toft.core2.DBException;
import com.toft.core2.ToftException;
import com.toft.core2.dao.SqlUtils;
import com.toft.core2.service.AbstractService;
import com.toft.widgets.workflow.factory.ServiceFactory;
import com.toft.widgets.workflow.factory.impl.ServiceFactoryImpl;
import com.toft.widgets.workflow.model.CompleteTaskResult;
import com.toft.widgets.workflow.provider.ApprocalAssistance;
import com.toft.widgets.workflow.service.IWorkflowService;
import com.toft.widgets.workflow.utils.ConstantUtil;
import com.wipz.eoa.iphone.BaseServiceImpl;
import com.wipz.eoa.iphone.util.Constants;

/**
 * @author lllianga
 * @date 2011-5-21下午02:45:38
 * @version 1.0
 */
@SuppressWarnings("all")
public class WorkFlowCoreService extends BaseServiceImpl {

	public static ServiceFactory serviceFactory = ServiceFactoryImpl
			.getServiceFactory();
	public static IWorkflowService workflowService = serviceFactory
			.getWorkflowService();

	public CompleteTaskResult submitProcess(String agencyId,
			String businessType, Map variables, String userCode)
			throws ToftException {
		CompleteTaskResult result = null;
		try {
			ApprocalAssistance.beginTransaction();
			result = workflowService.startProcessInstanceByMountInfo(agencyId,
					businessType, variables, userCode);
			ApprocalAssistance.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
			ApprocalAssistance.rollbackTransaction();
			throw ToftException.ThrowToftException(e.getMessage());
		}
		return result;
	}

	public CompleteTaskResult reSubmitProcess(String agencyId,
			String businessType, Map variables, String userCode)
			throws ToftException {
		CompleteTaskResult result = null;
		try {
			ApprocalAssistance.beginTransaction();
			result = workflowService.reSubmitProcessInstance(agencyId,
					businessType, variables, userCode);
			ApprocalAssistance.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
			ApprocalAssistance.rollbackTransaction();
			throw ToftException.ThrowToftException(e.getMessage());
		}
		return result;
	}

	public CompleteTaskResult complete(String userCode, String taskId,
			String transition, Map variable, boolean flag, String uuitId) {
		CompleteTaskResult completeTaskResult = null;
		try {
			ApprocalAssistance.beginTransaction();
			// 调用接口完成任务
			variable.put(ConstantUtil.AGENCY_ID, uuitId);
			completeTaskResult = workflowService.completeTask(userCode, taskId,
					transition, variable, true);

			ApprocalAssistance.commitTransaction();

		} catch (Exception e) {
			e.printStackTrace();
			ApprocalAssistance.rollbackTransaction();
		}
		return completeTaskResult;
	}

	/**
	 * 提交值，将单据编号、流程ID、业务类型ID保存到第三张表
	 * 
	 * @param params
	 *            :参数集合(单据编号、流程ID、业务类型ID)
	 * @throws ToftException
	 */
	public void saveProcess(Map params) throws ToftException {
		super.insert("businessProcess.insert", params);
	}

	/**
	 * 查看单据详情时，获取流程ID
	 * 
	 * @param params
	 * @return:params
	 * @throws ToftException
	 */
	public Map queryProcessID(Map params) throws ToftException {
		Map newMap = (Map) super
				.queryOne("businessProcess.queryByCode", params);
		String processId = newMap != null ? (String) newMap.get("PROCESSID")
				: "";
		params.put("processInstanceId", processId);
		return params;
	}

	public String submit(String businessid,String uuid,String billCode,String COMMITERCODE,String agencyId,String remark) {
		Map putMap = new HashMap();
		putMap.put(ConstantUtil.BUSINESS_RECORE_ID, uuid);
		putMap.put(ConstantUtil.BUSINESS_SN, billCode);
		putMap.put(ConstantUtil.BUSINESS_MONEY, remark.toString());
		putMap.put("role", "0");

		String nextApprovaler = "";

		Map usermap = (Map) super.queryOne("user.queryUserList", COMMITERCODE);
		String userid = (String) usermap.get("ID");
		// 查询 职责ID

		Map mapparams = new HashMap();
		mapparams.put("USERID", userid);
		mapparams.put("AGENCYID", agencyId);
		Map r = new HashMap();
		r = (Map) super.queryOne("agency.queryDutyIDByUserIDAgencyID",
				mapparams);
		String dutyId = (String) r.get("DUTYID");

		putMap.put("dutyId", dutyId);
		putMap.put("agencyId", agencyId);
		putMap.put("userCode", COMMITERCODE);

		// 流程编号，如果存在则为重新提交，如果为null则为新单据
		String processinstanceID = null;
		// 执行任务后的结果参数
		CompleteTaskResult completeTaskResult = null;
		Map newParams = new HashMap();
		Iterator iterator = putMap.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry entry = (Entry) iterator.next();
			newParams.put(entry.getKey(), entry.getValue());
		}

		try {
			completeTaskResult = this.submitProcess(agencyId,
					businessid, newParams, COMMITERCODE);
		} catch (ToftException e) {
			e.printStackTrace();
		}
		processinstanceID = (String) completeTaskResult.getProcessInstanceId(); // 获得工作流实例ID
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentDate = sdf.format(new Date());

		Map processMap = new HashMap();
		processMap.put("BILLID", billCode);
		processMap.put("PROCESSID", processinstanceID);
		processMap.put("BUSINESSTYPE", businessid);
		super.insert("businessProcess.insert", processMap);

		// 查询下级审批人
		nextApprovaler = (String) completeTaskResult.getNextOperators();
		return nextApprovaler;
	}
}
